<?php
    require_once('popup_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class popup_select_bid extends popup_base_jectly {

        protected $contents_html='<div class=\'center\'>This is popup_select_bid page.</div>';

        protected $id;//project_id
        public $project;
        protected $uid;//provider's user_id // this can be null
        public $provider;//TableUserProject this can be null
 		public $bid;//this can be unique only by project id but will check from query string too
		public $bool_confirming=false;
		public $bool_message_exist=false;
		public $bool_error=false;
		public $bool_permitted=false;
		public $button_confirm;
		public $button_cancel;
		public $bid_cancel;
		public $textbox_comment;
		public $lstProvider;
		public $str_permission_error_text;
		public $objProvider;
		public $objectProviderArray;
		public $biddersArray;
		public $TableProjectBid;
		public $objBidUser;

        function __construct() {
            parent::__construct();
        }

        protected function Form_Create() {

            $this->check_query_string();

            $this->load_project();
        
            if($this->bool_select_bid_permitted())
		{
			$this->bool_permitted=true;


		}


		if($this->bool_permitted)
		{

			$this->button_confirm_Create();
			$this->button_cancel_Create();
			$this->textbox_comment_Create();
			$this->lstProvider_Create();


		}
		}
		protected function textbox_comment_Create() {
		$this->textbox_comment = new QTextBox($this);
		$this->textbox_comment->Width=400;
		$this->textbox_comment->Columns=50;
		$this->textbox_comment->Rows=10;
		$this->textbox_comment->Name = 'comment';
		//$this->textbox_comment->CssClass = 'input';
		$this->textbox_comment->Visible=true;
		$this->textbox_comment->Wrap=true;
		$this->textbox_comment->TextMode=QTextMode::MultiLine;
		$this->textbox_comment->Overflow=QOverflow::Auto;
		if($this->bool_confirming)$this->textbox_comment->Visible=false;
	}	
	
	protected function lstProvider_Create($strcontrolId=null)
	{	
		$TableProjectBid= TableProject::LoadById($this->id);
		$this->lstProvider = new QRadioButtonList($this);
			$this->lstProvider->Name = QApplication::Translate('Provider');
			$this->lstProvider->Required = true;
			$objBidArray=$TableProjectBid->load_bids_all_for_the_project_order_by_bid_date();
			if ($objBidArray) foreach ($objBidArray as $objBidUser) {
				$user=TableUserProject::loadByID($objBidUser->UserId);
				$objListItem = new QListItem(' [Provider Name: '.$user->UserName. '] [Bid:'.$objBidUser->Bid.'][Days To Complete:'.$objBidUser->DayToComplete.']', $objBidUser->Id);
				$this->lstProvider->AddItem($objListItem);
				
			}
			return $this->lstProvider;
	  	}
	

	
	
	

	protected function button_confirm_Create() {
		$this->button_confirm = new QButton($this);
		$this->button_confirm->Text = 'confirm';
		$this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
		$this->button_confirm->PrimaryButton = true;
	}
	protected function button_cancel_Create() {
		$this->button_cancel = new QButton($this);
		$this->button_cancel->Text = 'cancel';
		$this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
		//$this->button_cancel->PrimaryButton = true;
	}
	public function confirm_Click($strFormId, $strControlId, $strParameter) {

		$objUser = unserialize($_SESSION['User']);
		$this->UserId=$objUser->Id;
		$objTableProjectBid=TableProjectBid::LoadById($this->lstProvider->SelectedValue);
		$tableprojectWon= new TableProjectWon();
		$tableprojectWon->ProjectId=$this->id;
		$tableprojectWon->BuyerId=$objTableProjectBid->BuyerId;
		$tableprojectWon->ProviderId=$objTableProjectBid->UserId;
		$tableprojectWon->Bid=$objTableProjectBid->Bid;
		$tableprojectWon->Day=$objTableProjectBid->DayToComplete;
		$tableprojectWon->set_won_date_for_now();
		$tableprojectWon->set_status_before_accept();	
		$tableprojectWon->Save();


		QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');
	}
   public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }
		
		

        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }

            if(is_numeric(QApplication::QueryString('uid')))
            {
                $this->uid=QApplication::QueryString('uid');//provider_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
            
          		$this->project=TableProject::LoadById($this->id);
				
                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.']';
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }
            public function render_confirming_text() {

            if($this->bool_confirming)
            {
                echo($this->str_confirming_text);
            }
        }
        
          public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }
         public function render_message_text() {

            if($this->bool_message_exist)
            {
                echo($this->str_message);
            }
        }

        //if provider is loaded,the provider will be selected in the list when loading the page
        protected function load_provider()
        {
            if(isset($this->uid) && is_numeric($this->uid))
            {
                $this->provider=TableUserProject::LoadById($this->uid);

                if(isset($this->provider))
                {
                    $this->contents_html.='provider loaded-['.$this->provider->UserName.'] user_id=['.$this->provider->UserId.']';
                }
                else
                {
                    $this->contents_html.='provider not found id=['.$this->uid.']';
                    $this->uid=null;
                    $this->provider=null;
                }
            }
        }

        //check permissions
        public function bool_project_loaded()
        {
            return isset($this->project);
        }

        public function bool_provider_loaded()
        {
            return isset($this->provider);
        }

        public function bool_my_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        public function bool_won_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }

            return false;
        }

        protected function bool_already_bidding()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_bidding_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        protected function bool_already_won()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if(TableProject::bool_won_for_the_project($this->UserId,$this->id))
                {
                    return true;
                }
            }

            return false;
        }

        public function bool_project_open()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }

        public function bool_project_after_picked()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }

        public function bool_project_bidding_end()
        {
            if(!bool_project_open() && !bool_project_after_picked())return true;

            return false;
        }

        public function bool_bid_exist()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                if($this->project->count_bid_for_this_project()>0)
                {
                    return true;
                }
            }

            return false;
        }

        protected function bool_select_bid_permitted()
        {/*
            if(!$this->bool_logined())return false;
            if(!$this->bool_project_loaded())return false;
            if(!$this->bool_my_project())return false;
            if(!$this->bool_project_open())return false;
            if(!$this->bool_bid_exist())return false;

            if(TableProject::bool_can_pick_for_the_project($this->UserId,$this->id))
            {
                return true;
            }*/

            return true;
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }

	}

    popup_select_bid::Run('popup_select_bid','popup_select_bid.tpl.php');

?>